package com.Babysleep.Activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

import com.Babysleep.R;
import com.Babysleep.Interface.MyGesture;
import com.Babysleep.manage.Listen;

/**
 * 新activity的基类，主要实现了一些按钮的监听等功能。
 * 
 */

public class BasicActivity extends Activity {
	public int cycle_length = 0;
	public int name;
	public Listen player;
	GestureDetector mGesture;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mGesture = new GestureDetector(new MyGesture(this));
		name = getIntent().getIntExtra("name", 0);
	}

	/**
	 * 返回第一主页list效果
	 */
	public void back_to_list(ImageButton button) {
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), List.class);
				startActivity(intent);
			}
		});
	}

	/**
	 * 退出当前activity，返回列表上一activity
	 */
	public void quit_this_activity(ImageButton button) {
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	public void quit_this_activity(Button button) {
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	/**
	 * 返回列表activity
	 */


	/**
	 * 选择循环模式
	 */
	public void set_cycle() {
		switch (cycle_length) {
		case 0:
			cycle_length = 300;
			((ImageButton) findViewById(R.id.cycle))
					.setImageDrawable(getResources().getDrawable(
							R.drawable.cycle5));
			break;
		case 300:
			cycle_length = 900;
			((ImageButton) findViewById(R.id.cycle))
					.setImageDrawable(getResources().getDrawable(
							R.drawable.cycle15));
			break;
		case 900:
			cycle_length = 1800;
			((ImageButton) findViewById(R.id.cycle))
					.setImageDrawable(getResources().getDrawable(
							R.drawable.cycle30));
			break;
		case 1800:
			cycle_length = 0;
			((ImageButton) findViewById(R.id.cycle))
					.setImageDrawable(getResources().getDrawable(
							R.drawable.cycle0));
			break;
		default:
			break;
		}
		player.set_counter(cycle_length);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case 4:

			if (player != null) {
				if (player.isPlaying()) {
					player.Stop();
				}
				player.release();
				player = null;
				finish();
				back_to_list();
			}

			break;

		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	
	public void back_to_list() {
		Intent intent = new Intent(getApplicationContext(), Main.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mGesture.onTouchEvent(event);
	}
}